package com.example.controller;

import com.example.entity.CurrentUser;
import com.example.entity.OutStore;
import com.example.entity.Result;
import com.example.entity.Store;
import com.example.page.Page;
import com.example.service.OutStoreService;
import com.example.service.StoreService;
import com.example.utils.TokenUtils;
import com.example.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author:tjf
 * @Description:
 */
@RequestMapping("/outstore")
@RestController
public class OutStoreController {
    @Autowired
    private OutStoreService outStoreService;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private StoreService storeService;
    /**
     * 添加出库单接口url/outstore/outstore-add
     * @param outStore
     * @param token
     * @return
     */
    @RequestMapping("/outstore-add")
    public Result saveOutStore(@RequestBody OutStore outStore,
                         @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        // 获取当前登录用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        // 封装参数outStore
        outStore.setCreateBy(currentUser.getUserId());

        // 执行添加出库单的业务方法
        Result result = outStoreService.saveOutStore(outStore);

        //响应
        return result;
    }

    /**
     * 获取仓库列表接口url/outstore/store-list
     * @return
     */
    @RequestMapping("/store-list")
    public Result getStoreList(){
        //1 查询所有的仓库
        List<Store> storeList = storeService.queryAllStore();

        return Result.ok(storeList);
    }

    /**
     * 根据条件查询出库单并分页接口url/outstore/outstore-page-list
     * @param outStore
     * @param page
     * @return
     */
    @RequestMapping("/outstore-page-list")
    public Result queryOutStoreByConditionForPagination(OutStore outStore, Page page){
        //1 执行根据条件查询出库单的业务方法
        outStoreService.queryOutStoreByConditionForPagination(outStore, page);

        //2 响应
        return Result.ok(page);
    }

    /**
     * 确定商品出库接口url/outstore/outstore-confirm
     * @param outStore
     * @return
     */
    @RequestMapping("/outstore-confirm")
    private Result confirmOutStoreIsOut(@RequestBody OutStore outStore,
                      @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //1 获取当前登录用户的信息并作为商品的updateBy字段的值
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);

        //2 执行商品出库的业务方法
        Result result = outStoreService.confirmOutStoreIsOutById(outStore, currentUser.getUserId());

        return result;
    }
}
